#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pygame
from config import *

class MapSelector:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("选择地图")
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont(None, 48)
        self.title_font = pygame.font.SysFont(None, 64)
        
        # 选项位置和大小 - 增加到3个选项
        self.option_rects = [
            pygame.Rect(SCREEN_WIDTH//4, SCREEN_HEIGHT//4, SCREEN_WIDTH//2, 60),
            pygame.Rect(SCREEN_WIDTH//4, SCREEN_HEIGHT//4 + 80, SCREEN_WIDTH//2, 60),
            pygame.Rect(SCREEN_WIDTH//4, SCREEN_HEIGHT//4 + 160, SCREEN_WIDTH//2, 60)
        ]
        self.selected_option = 0  # 0表示地图1，1表示地图2，2表示地图3
        
    def draw(self):
        self.screen.fill(BLACK)
        
        # 绘制标题
        title = self.title_font.render("Select a Map", True, WHITE)
        title_rect = title.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//8))
        self.screen.blit(title, title_rect)
        
        # 绘制选项 - 增加地图3选项
        options = [
            "MAP1 - Classics", 
            "MAP2 - Fracture",
            "MAP3 - Egg"
        ]
        for i, (rect, text) in enumerate(zip(self.option_rects, options)):
            # 选中项高亮显示
            color = GREEN if i == self.selected_option else GRAY
            pygame.draw.rect(self.screen, color, rect)
            pygame.draw.rect(self.screen, WHITE, rect, 2)  # 边框
            
            # 绘制文字
            text_surface = self.font.render(text, True, WHITE)
            text_rect = text_surface.get_rect(center=rect.center)
            self.screen.blit(text_surface, text_rect)
        
        pygame.display.update()
    
    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    return None
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        self.selected_option = (self.selected_option - 1) % 3  # 修改为3个选项
                    elif event.key == pygame.K_DOWN:
                        self.selected_option = (self.selected_option + 1) % 3  # 修改为3个选项
                    elif event.key == pygame.K_RETURN:
                        # 返回选中的地图编号（1、2或3）
                        return self.selected_option + 1
                if event.type == pygame.MOUSEBUTTONDOWN:
                    # 鼠标点击检测
                    for i, rect in enumerate(self.option_rects):
                        if rect.collidepoint(event.pos):
                            return i + 1
            
            self.draw()
            self.clock.tick(FPS)